package csc.training.jsf2.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import csc.training.jsf2.entity.EmailInfo;
import csc.training.jsf2.service.MailManageService;


/**
 * 
 * @author hdang20
 *
 */
public class MailManageBean implements Serializable{
	private static final long serialVersionUID = 1001006494703114049L;
	private MailManageService mailManageService;
	private String recordsPerPage = "10";
	private int pageNumber = 1;	
	private String contentForSearch;
	private String workon = "Checking Inbox Mail";
	private List<EmailInfo> emails = null;
	private LoginManageBean loginManageBean;
	private int mode = 1; // checking inbox mail
	private final Locale ENGLISH = Locale.ENGLISH;
	private final Locale FRANCE = new Locale("fr");
	
	private Boolean selectAll;
	
	public Boolean getSelectAll() {
		return selectAll;
	}

	public void setSelectAll(Boolean selectAll) {
		this.selectAll = selectAll;
	}

	private Map<Integer, Boolean> selectedIds = new HashMap<Integer, Boolean>();
	public Map<Integer, Boolean> getSelectedIds() {
	    return selectedIds;
	}
	
	public void setSelectedIds(Map<Integer, Boolean> selectedIds) {
		this.selectedIds = selectedIds;
	}

	private Locale currentLocal = ENGLISH;
	
	private String language = "English";

	public LoginManageBean getLoginManageBean() {
		return loginManageBean;
	}

	public void setLoginManageBean(LoginManageBean loginBean) {
		this.loginManageBean = loginBean;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public Locale getCurrentLocal() {
		if(language.equals("English")){
			currentLocal = ENGLISH;
		}else{
			currentLocal = FRANCE;
		}
		return currentLocal;
	}

	public void setCurrentLocal(Locale currentLocal) {
		this.currentLocal = currentLocal;
	}

	public String editProfile(){
		
		return ("profiles?faces-redirect=true");
	}
	
	public String getWorkon(){
		return workon;
	}
	
	public List<EmailInfo> getMailList(int mode){
		if(loginManageBean.getAccount() == null){
			try{
				FacesContext.getCurrentInstance().getExternalContext().dispatch("login.jsf");
			}catch(IOException ex){
				
			}			
		}

		List<EmailInfo> emails = mailManageService.getInboxMails(contentForSearch);
		if(mode == 2){
			emails = mailManageService.getSentMails(contentForSearch);
		}else if(mode == 3){
			emails = mailManageService.getDeletedMails(contentForSearch);
		}
		
		if(!"All".equals(recordsPerPage)){
			int startIndex = Integer.parseInt(recordsPerPage)*(pageNumber-1);
			if(startIndex < 0){
				startIndex = 0;
			}
			int endIndex = startIndex+Integer.parseInt(recordsPerPage);
			if(endIndex > emails.size()){
				endIndex = emails.size();
			}
			List<EmailInfo> email1 = new LinkedList<EmailInfo>();
			for(int i=startIndex;i<endIndex;i++){
				email1.add(emails.get(i));
			}
			return email1;
		}

		return emails;		
	}
	
	public void setEMails(List<EmailInfo> emails){
		this.emails = emails;
	}
	
	public List<EmailInfo> getEmails(){
		if(emails == null){
			emails = getMailList(1);
		}
		return this.emails;
	}
	public String doNextPage(){
		pageNumber++;
		List<EmailInfo> emails = getMailList(mode);
		setEMails(emails);
		setSelectAll(false);
		return "mailViewer";
	}
	
	public String doPreviousPage(){
		pageNumber--;
		List<EmailInfo> emails = getMailList(mode);
		setEMails(emails);
		setSelectAll(false);
		return "mailViewer";
	}

	public void setMailManageService(MailManageService mailManageService) {
		this.mailManageService = mailManageService;
	}

	public String getRecordsPerPage() {
		return recordsPerPage;
	}

	public void setRecordsPerPage(String recordsPerPage) {
		this.recordsPerPage = recordsPerPage;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public boolean isFirstPage() {
		return pageNumber <= 1?true:false;
	}

	public boolean isLastPage() {
		if(recordsPerPage.equals("All")){
			return true;
		}
		List<EmailInfo> emails = mailManageService.getInboxMails(contentForSearch);
		if(mode == 2){
			emails = mailManageService.getSentMails(contentForSearch);
		}else if(mode == 3){
			emails = mailManageService.getDeletedMails(contentForSearch);
		}
		return pageNumber*Integer.parseInt(recordsPerPage) >= emails.size()?true:false;
	}	
	public int getNotYetReadMailNumber(){
		List<EmailInfo> emails = mailManageService.getEmails();
		int count = 0;
		for (EmailInfo emailInfo : emails) {
			if(emailInfo.getStatus().equals(EmailInfo.NOT_YET_READ)){
				count++;
			}
		}
		return count;
	}
	
	public int getSentMailNumber(){
		List<EmailInfo> emails = mailManageService.getEmails();
		int count = 0;
		for (EmailInfo emailInfo : emails) {
			if(emailInfo.getStatus().equals(EmailInfo.SENT)){
				count++;
			}
		}
		return count;
	}
	
	public int getDeletedMailNumber(){
		List<EmailInfo> emails = mailManageService.getEmails();
		int count = 0;
		for (EmailInfo emailInfo : emails) {
			if(emailInfo.getStatus().equals(EmailInfo.DELETED)){
				count++;
			}
		}
		return count;
	}
	
	public String checkInboxMail(){
		workon = "Checking Inbox Mail";
		pageNumber = 1;
		mode = 1;
		List<EmailInfo> emails = getMailList(mode);
		setEMails(emails);
		setSelectAll(false);
		return "mailViewer";
	}
	public String checkSentMail(){
		workon = "Checking Sent Mail";
		pageNumber = 1;
		mode = 2;
		List<EmailInfo> emails = getMailList(mode);
		setEMails(emails);
		setSelectAll(false);
		return "mailViewer";
	}
	public String checkDeletedMail(){
		workon = "Checking Deleted Mail";
		pageNumber = 1;
		mode = 3;
		List<EmailInfo> emails = getMailList(mode);
		setEMails(emails);
		setSelectAll(false);
		return "mailViewer";
	}
	
	public void changeRecordsPerPage(javax.faces.event.ValueChangeEvent value){
		recordsPerPage = value.getNewValue().toString();
		if(mode == 1){
			checkInboxMail();
		}else if(mode == 2){
			checkSentMail();
		}else{
			checkDeletedMail();
		}
		//FacesContext.getCurrentInstance().renderResponse();
		FacesContext.getCurrentInstance().getViewRoot();
		
	}

	public String getContentForSearch() {
		return contentForSearch;
	}

	public void setContentForSearch(String contentForSearch) {
		this.contentForSearch = contentForSearch;
	}
	
	public String searchAction(){
		pageNumber = 1;
		List<EmailInfo> emails = getMailList(mode);
		setEMails(emails);
		return "mailViewer";
	}

	public String deleteEmails(){
		List<EmailInfo> emails = mailManageService.getEmails();
		Map<Integer, Boolean> selectedIds =  getSelectedIds();
		for(Integer id : selectedIds.keySet()){
			if(selectedIds.get(id)){
				for(int j = 0;j<emails.size();j++){
					if(emails.get(j).getId() == id){
						emails.remove(j);
						break;
					}
				}
			}
		}

		setEMails(getMailList(mode));
		selectAll = false;
		return "mailViewer";
	}
	
   public void switchLocale(ValueChangeEvent value) {
	   
	    String locale = value.getNewValue().toString();
	    if (locale.equals("English")) {
	    	currentLocal = ENGLISH;
	    } else {
	    	currentLocal = FRANCE;
	    }
	    FacesContext.getCurrentInstance().getViewRoot().setLocale(currentLocal);
  }
   
   public String logout() {
//	    FacesContext fc = FacesContext.getCurrentInstance();
//	    ExternalContext externalContext = fc.getExternalContext();
//	    Map<String, Object> map = externalContext.getSessionMap();
//	    map.get("loginBean");
	    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

	    return "login"; 
	}

	
}
